package cc.chinagps.gateway.unit.common.bsj;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cc.chinagps.gateway.buff.GBossDataBuff.BaseStationInfo;
import cc.chinagps.gateway.buff.GBossDataBuff.GpsBaseInfo;
import cc.chinagps.gateway.buff.GBossDataBuff.WifiAPInfo;
import cc.chinagps.gateway.unit.beans.GbossGpsInfo;
import lombok.Data;

@Data
public class BsjRequest {

	String wifiStr;
	String lbsStr;
	String manStr;
	String macs;
	String moreLbs;

	private String toMac(String s) {
		int len = s.length();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len / 2; i++) {
			sb.append(s.substring(i * 2, i * 2 + 2));
			if (i != (len / 2 - 1)) {
				sb.append(":");
			}
		}
		return sb.toString();
	}

	private String getMacs(List<WifiAPInfo> wifiAPInfos) {

		if (wifiAPInfos == null || (wifiAPInfos.size() == 0)) {
			return null;
		}
		int size = wifiAPInfos.size();
		int index = 0;
		StringBuilder sb = new StringBuilder();
		for (WifiAPInfo wifiAPInfo : wifiAPInfos) {
			sb.append(toMac(wifiAPInfo.getMac()));
			sb.append(",");
			sb.append(wifiAPInfo.getSignalIntensity());
			if (index != size - 1) {
				sb.append("|");
			}
			index++;
		}
		return sb.toString();
	}

	private String getWifiStr(List<WifiAPInfo> wifiAPInfos) {
		
		if (wifiAPInfos == null || wifiAPInfos.size() == 0) {
			return null;
		}
		int size = wifiAPInfos.size();
		int index = 0;
		StringBuilder sb = new StringBuilder();
		for (WifiAPInfo wifiAPInfo : wifiAPInfos) {
			sb.append(toMac(wifiAPInfo.getMac()));
			sb.append(",");
			sb.append(wifiAPInfo.getSignalIntensity());
			sb.append(",abc");
			if (index != size - 1) {
				sb.append("|");
			}
			index++;
		}
		return sb.toString();
	}

	private String getLbsStr(List<BaseStationInfo> baseStationInfos) {
		
		if (baseStationInfos == null || (baseStationInfos.size() == 0)) {
			return null;
		}
		int size = baseStationInfos.size();
		int index = 0;
		StringBuilder sb = new StringBuilder();
		for (BaseStationInfo baseStationInfo : baseStationInfos) {
			sb.append(baseStationInfo.getMcc());
			sb.append(";");
			sb.append(baseStationInfo.getMnc());
			sb.append(";");
			sb.append(baseStationInfo.getLac());
			sb.append(",");
			sb.append(baseStationInfo.getCid());
			sb.append(",");
			sb.append(baseStationInfo.getBsss());
			if (index != size - 1) {
				sb.append("|");
			}
			index++;
		}
		return sb.toString();
	}

	private String getMoreLbs(List<BaseStationInfo> baseStationInfos) {
		
		if (baseStationInfos == null || (baseStationInfos.size() == 0)) {
			return null;
		}
		int index = 0;
		int size = baseStationInfos.size();
		StringBuilder sb = new StringBuilder();
		for (BaseStationInfo baseStationInfo : baseStationInfos) {
			if (index == 0) {
				sb.append(baseStationInfo.getMnc());
				sb.append("@6@");
			}
			sb.append(baseStationInfo.getLac());
			sb.append(",");
			sb.append(baseStationInfo.getCid());
			sb.append(",");
			sb.append(baseStationInfo.getBsss());
			if (index != size - 1) {
				sb.append(";");
			}
			index++;
		}
		return sb.toString();
	}

	private String getManStr(List<BaseStationInfo> baseStationInfos) {
		if (baseStationInfos == null || (baseStationInfos.size() == 0)) {
			return null;
		}
		BaseStationInfo baseStationInfo = baseStationInfos.get(0);
		StringBuilder sb = new StringBuilder();
		sb.append(baseStationInfo.getMcc());
		sb.append(";");
		sb.append(baseStationInfo.getMnc());
		sb.append(";");
		sb.append(baseStationInfo.getLac());
		sb.append(",");
		sb.append(baseStationInfo.getCid());
		sb.append(",");
		sb.append(baseStationInfo.getBsss());
		return sb.toString();
	}

	public BsjRequest parse(GbossGpsInfo gps) {
		BsjRequest bsjRequest = new BsjRequest();
		GpsBaseInfo gpsBase = gps.getGps().getBaseInfo();
		List<BaseStationInfo> list = gpsBase.getBaseStationsList();
		List<WifiAPInfo> wifiAPInfos = gpsBase.getWifiAPInfosList();
		bsjRequest.setLbsStr(getLbsStr(list));
		bsjRequest.setWifiStr(getWifiStr(wifiAPInfos));
		bsjRequest.setManStr(getManStr(list));
		bsjRequest.setMacs(getMacs(wifiAPInfos));
		bsjRequest.setMoreLbs(getMoreLbs(list));
		return bsjRequest;
	}

	public Map<String, Object> toWifiAndLbsPostParams() {
		Map<String, Object> params = new HashMap<>();
		params.put("wifiStr", this.wifiStr);
		params.put("lbsStr", this.lbsStr);
		params.put("manStr", this.manStr);
		return params;
	}

	public Map<String, Object> toLbsPostParams() {
		Map<String, Object> params = new HashMap<>();
		params.put("moreLbs", this.moreLbs);
		return params;
	}

	public Map<String, Object> toWifiPostParams() {
		Map<String, Object> params = new HashMap<>();
		params.put("macs", this.macs);
		return params;
	}

}
